package cn.jly.spring.generic_application_context;

import org.springframework.context.support.GenericApplicationContext;

import java.util.Arrays;
import java.util.function.Supplier;

/**
 * @author lanyangji
 * @description 函数式风格创建对象，交给spring管理
 * @date 2020/11/11 下午 9:06
 */
public class GenericApplicationContextTest {
    public static void main(String[] args) {
        // 1. 创建GenericApplicationContext对象
        final GenericApplicationContext context = new GenericApplicationContext();
        // 2. 调用context的方法对象注册
         context.refresh();
        // spring5新功能，通过lambda表达式注册bean
        context.registerBean(User.class, new Supplier<User>() {
            @Override
            public User get() {
                return new User(1101L, "tom");
            }
        });

        // 3. 获取自己手动注册的bean
        final User user = context.getBean(User.class);
        System.out.println("user = " + user);

        // 获取名字
        final String[] names = context.getBeanNamesForType(User.class);
        System.out.println(Arrays.toString(names));

        context.close();
    }
}
